<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-col :md="12" :sm="12">
          操作人：{{nickname()}}
        </a-col>
        <a-col :md="12" :sm="12">
          操作日期：{{this.modDate}}
        </a-col>
        <a-divider/>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方总部编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag disabled v-decorator="['purchaserHqCode',validatorRules.purchaserHqCode,{}]" :triggerChange="true" placeholder="请选择" dictCode="purchaserHqCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="供应商名称" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag @change="handleInput" v-decorator="[ 'supplierCode', validatorRules.supplierCode, {}]" :triggerChange="true" placeholder="请选择供应商名称" dictCode="supplierCode" />
            <a-input v-model="model.supplierName" type="hidden"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="销售订单号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input disabled v-decorator="[ 'soNo', validatorRules.soNo]" placeholder="请输入销售订单号"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方公司编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'buyerCode', validatorRules.buyerCode]" placeholder="请输入采购方公司编码"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方公司名称" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'buyerName', validatorRules.buyerName]" placeholder="请输入采购方公司名称"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方区域" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'buyerArea', validatorRules.buyerArea]" placeholder="请输入采购方区域"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购方公司省份" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'buyerProvince', validatorRules.buyerProvince]" placeholder="请输入采购方公司省份"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="品类编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag disabled v-decorator="['categoryCode',validatorRules.categoryCode,{}]" :triggerChange="true" placeholder="请选择品类编码" dictCode="categoryCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="种类编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag disabled v-decorator="['subclassCode',validatorRules.subclassCode,{}]" :triggerChange="true" placeholder="请选择种类编码" dictCode="subclassCode"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="销售订单状态" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'soStatus', validatorRules.soStatus]" placeholder="请输入销售订单状态"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据来源" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag disabled v-decorator="['dataSource',{}]" :triggerChange="true" placeholder="请选择数据来源" dictCode="dataSource"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据来源创建时间" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-date disabled showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" v-decorator="[ 'dataSourceCreateTime', validatorRules.dataSourceCreateTime]" placeholder="请输入数据来源创建时间"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="合同编号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'conCode', validatorRules.conCode]" placeholder="请输入合同编号"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="删除标志" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <j-dict-select-tag disabled v-decorator="['isDeleted',validatorRules.isDeleted,{}]" :triggerChange="true" placeholder="请选择删除标志" dictCode="isDeleted"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="更新时间" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input disabled v-decorator="[ 'modifyTime', validatorRules.modifyTime]" placeholder="请输入更新时间"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="销售订单行项目号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input disabled v-decorator="[ 'soItemNo', validatorRules.soItemNo]" placeholder="请输入销售订单行项目号"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="关联方式" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
<!--            <j-dict-select-tag disabled v-decorator="['supplierSoRelType',validatorRules.supplierSoRelType,{}]" :triggerChange="true" placeholder="请选择关联方式" dictCode="supplierSoRelType"/>-->
            <a-input disabled v-decorator="[ 'supplierSoRelType', validatorRules.supplierSoRelType]" placeholder="请输入关联方式"/>

          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购订单id或者采购订单行项目id" :label-col="{ span:15 }" :wrapper-col="{ span: 9 }">
            <a-input disabled v-decorator="[ 'eipPoNo', validatorRules.eipPoNo]" placeholder="请输入采购订单id或者采购订单行项目id"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购订单行项目ID" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input disabled v-decorator="[ 'poItemId', validatorRules.poItemId]" placeholder="请输入采购订单行项目ID"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物资编码" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'productCode', validatorRules.productCode]" placeholder="请输入物资编码"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物资名称" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'productName', validatorRules.productName]" placeholder="请输入物资名称"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物资单位" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'productUnit', validatorRules.productUnit]" placeholder="请输入物资单位"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购订单厂家计划交货日期" :label-col="{ span:12 }" :wrapper-col="{ span: 12 }">
            <j-date v-decorator="[ 'planDeliveryDate', validatorRules.planDeliveryDate]" placeholder="请输入采购订单厂家计划交货日期"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="采购订单厂家发货地点" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'planDeliveryPlace', validatorRules.planDeliveryPlace]" placeholder="请输入采购订单厂家发货地点"/>
          </a-form-item>
        </a-col>
        <a-col :md="24" :sm="24">
          <a-form-item label="采购订单厂家发货地点描述" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'planDeliveryPlaceDesc', validatorRules.planDeliveryPlaceDesc]" placeholder="请输入采购订单厂家发货地点描述"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物资数量" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'productAmount', validatorRules.productAmount]" placeholder="请输入物资数量"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="物资id分组编号" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'productIdGrpNo', validatorRules.productIdGrpNo]" placeholder="请输入物资id分组编号"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据拥有方" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'ownerId', validatorRules.ownerId]" placeholder="请输入数据拥有方"/>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="12">
          <a-form-item label="数据可见方" :label-col="{ span:8 }" :wrapper-col="{ span: 16 }">
            <a-input v-decorator="[ 'openId', validatorRules.openId]" placeholder="请输入数据可见方"/>
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import { mapGetters } from 'vuex'
  import { formatDate } from '@/utils/util'
  import {initDictOptions, filterDictText} from '@/components/dict/JDictSelectUtil'

  export default {
    name: 'EipSalesOrderModal',
    components: {
      JDate
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        modDate: null,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        supplierCodeDictOptions:[],
        validatorRules: {
          purchaserHqCode: { rules: [{ required: true, max :8, message: '采购方总部编码不能为空或过长!' }] },
          supplierCode: { rules: [{ required: true, max :16, message: '供应商编码不能为空或过长!' }] },
          supplierName: { rules: [{ required: true, max :32, message: '供应商名称不能为空或过长!' }] },
          soNo: { rules: [{ required: true, max :16, message: '销售订单号不能为空或过长!' }] },
          buyerCode: { rules: [{ required: true, max :16, message: '采购方公司编码不能为空或过长!' }] },
          buyerName: { rules: [{ required: true, max :128, message: '采购方公司名称不能为空或过长!' }] },
          buyerArea: { rules: [{ required: false, max :64, message: '采购方区域过长!' }] },
          buyerProvince: { rules: [{ required: true, max :64, message: '采购方公司省份不能为空或过长!' }] },
          categoryCode: { rules: [{ required: true, max :8, message: '品类编码不能为空或过长!' }] },
          subclassCode: { rules: [{ required: true, max :8, message: '种类编码不能为空或过长!' }] },
          soStatus: { rules: [{ required: false, max :64, message: '订单状态过长!' }] },
          dataSource: { rules: [{ required: true, max :32, message: '数据来源不能为空或过长!' }] },
          dataSourceCreateTime: { rules: [{ required: false, max :32, message: '来源数据创建时间不能为空或过长!' }] },
          conCode: { rules: [{ required: true, max :32, message: '合同编号不能为空或过长!' }] },
          soItemNo: { rules: [{ required: true, max :32, message: '销售订单行项目号不能为空或过长!' }] },
          supplierSoRelType: { rules: [{ required: false, max :12, message: '销售订单与采购订单的关联方式过长!' }] },
          eipPoNo: { rules: [{ required: false, max :512, message: '采购订单id或者采购订单行项目id过长!' }] },
          poItemId: { rules: [{ required: false, max :512, message: '采购订单行项目ID过长!' }] },
          productCode: { rules: [{ required: true, max :32, message: '物资编码不能为空或过长!' }] },
          productName: { rules: [{ required: true, max :64, message: '物资名称不能为空或过长!' }] },
          productUnit: { rules: [{ required: true, max :32, message: '物资单位不能为空或过长!' }] },
          planDeliveryDate: { rules: [{ required: true, message: '采购订单厂家计划交货日期不能为空!' }] },
          planDeliveryPlace: { rules: [{ required: false, max :8, message: '采购订单厂家发货地点过长!' }] },
          planDeliveryPlaceDesc: { rules: [{ required: false, max :32, message: '采购订单厂家发货地点描述过长!' }] },
          productAmount: { rules: [{ required: true, max :32, message: '物资数量不能为空或过长!' }] },
          productIdGrpNo: { rules: [{ required: false, max :32, message: '物资id分组编号过长!' }] },
          ownerId: { rules: [{ required: false, max :16, message: '数据拥有方过长!' }] },
          openId: { rules: [{ required: false, max :16, message: '数据可见方过长!' }] },
          comment: { rules: [{ required: false, max :16, message: '报告批注过长!' }] },
          isDeleted: { rules: [{ required: false, max :8, message: '逻辑删除不能为空或过长!' }] },
          ioflag: { rules: [{ required: false, max :16, message: '状态表示过长!' }] },
          modifyTime: { rules: [{ required: false, max :32, message: '更新时间过长!' }] },
          message: { rules: [{ required: false, max :64, message: '错误信息过长!' }] },
          status: { rules: [{ required: false, max :8, message: '状态过长!' }] },
          transno: {},
          remark1: {},
          remark2: {},
          remark3: {},
          remark4: {},
          remark5: {}
        },
        url: {
          add: '/eipSalesOrder/add',
          edit: '/eipSalesOrder/editTemp'
        }

      }
    },
    created() {
      this.initDictConfig()
    },
    methods: {
      ...mapGetters(['nickname', 'avatar', 'userInfo']),
      handleInput(e){
        this.model.supplierName = filterDictText(this.supplierCodeDictOptions, e);
      },
      initDictConfig() {
        this.modDate = formatDate(new Date().getTime(), 'yyyy-MM-dd hh:mm:ss')
      },
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'puid', 'purchaserHqCode', 'soNo', 'supplierCode', 'supplierName', 'buyerCode', 'buyerName', 'buyerArea', 'buyerProvince', 'categoryCode', 'subclassCode', 'soStatus', 'dataSource', 'dataSourceCreateTime', 'conCode', 'isDeleted', 'modifyTime', 'soItemNo', 'supplierSoRelType', 'eipPoNo', 'poItemId', 'productCode', 'productName', 'productUnit', 'planDeliveryDate', 'planDeliveryPlace', 'planDeliveryPlaceDesc', 'productAmount', 'productIdGrpNo', 'ownerId', 'openId', 'ioflag', 'message', 'status', 'transno', 'remark1', 'remark2', 'remark3', 'remark4', 'remark5'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.puid) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'post'
            }
            let formData = Object.assign(this.model, values)
            formData['remark1'] = this.nickname()
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'puid', 'purchaserHqCode', 'soNo', 'supplierCode', 'supplierName', 'buyerCode', 'buyerName', 'buyerArea', 'buyerProvince', 'categoryCode', 'subclassCode', 'soStatus', 'dataSource', 'dataSourceCreateTime', 'conCode', 'isDeleted', 'modifyTime', 'soItemNo', 'supplierSoRelType', 'eipPoNo', 'poItemId', 'productCode', 'productName', 'productUnit', 'planDeliveryDate', 'planDeliveryPlace', 'planDeliveryPlaceDesc', 'productAmount', 'productIdGrpNo', 'ownerId', 'openId', 'ioflag', 'message', 'status', 'transno', 'remark1', 'remark2', 'remark3', 'remark4', 'remark5'))
      }
    },
    mounted() {
      initDictOptions('supplierCode').then((res) => {
        if (res.success) {
          this.supplierCodeDictOptions = res.result
        }
      });
    }
  }
</script>